#!/bin/bash

#定义一个函数get_char，其作用是按下任意键以继续。
get_char()
        {
        SAVEDSTTY=`stty -g`
        stty -echo
        stty cbreak
        dd if=/dev/tty bs=1 count=1 2> /dev/null
        stty -raw
        stty echo
        stty $SAVEDSTTY
        }


echo "
       使用方法：
       1、将脚本放到PicoSDK_Mobile_CoreLib、PicoSDK_PC_Runtime和PicoSDK_Unity同级目录
       2、PicoSDK_PC_Runtime编译完成后，将英文的PicoVR放到PicoSDK_PC_Runtime/English/下
       3、PicoSDK_Mobile_CoreLib编译完成后，将其两个分支下的so库、jar包分别放到build/MainUnity52和build/LegacyUnity目录下
       3、执行脚本，按照提示输入各个平台SDK的版本号"

echo "请输入Android_SDK版本号（例：0.6.2）。若不更新，请留空： "
read ANDROIDSDK_V
echo "请输入PC_SDK版本号（例：0.4.0）。若不更新，请留空： "
read PCSDK_V


echo "开始集成Android_SDK部分..."
if [[ -n ${ANDROIDSDK_V} ]] ; then    
#拷贝Runtime的PicoPlugin.dll到PicoSDK_Unity的MainUnity52分支之前,更新Unity的代码，
#       cd /data/local/PicoSDK_Unity
        cd D:/scmbuild/PicoSDK_Unity/
       git checkout . && git clean -xdf && git reset --hard HEAD && git checkout MainUnity52 && git pull
       #判断是否更新成功，成功则打印最新5条提交的commit时间、哈希和提交信息;失败则退出脚本并返回2
       if [[ $? -eq 0 ]] ; then
        echo -e "\n已更新PicoSDK_Unity的MainUnity52分支，最近的5条提交是:`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
        else
          echo "
failed:git pull MainUnity52分支失败！"
               exit 2
       fi
       #开始拷贝
       cp -f ../PicoSDK_Mobile_CoreLib/build/MainUnity52/libPicoPlugin.so Assets/Plugins/Android/libs/armeabi-v7a/libPicoPlugin.so
       cp -f ../PicoSDK_Mobile_CoreLib/build/MainUnity52/picolib.jar Assets/Plugins/Android/picolib.jar
       git add .
       git commit -m "update Android_SDK to ${ANDROIDSDK_V} on MainUnity52"
       git pull
       if [[ $? -eq 0 ]] ; then
        git push origin MainUnity52
       else
        echo "failed:在MainUnity52分支commit后git pull失败，请确认检查"
        exit 5
       fi

       git checkout . && git clean -xdf && git checkout LegacyUnity && git pull
       if [[ $? -eq 0 ]] ; then
               echo -e "\n已更新PicoSDK_Unity的LegacyUnity分支，当前所在提交:`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
        else
                echo -e "\nfailed:git pull LegacyUnity分支失败！"
         exit 6
       fi
       cp -f ../PicoSDK_Mobile_CoreLib/build/LegacyUnity/libPicoPlugin.so Assets/Plugins/Android/libs/armeabi-v7a/libPicoPlugin.so
       cp -f ../PicoSDK_Mobile_CoreLib/build/LegacyUnity/picolib.jar Assets/Plugins/Android/picolib.jar
       git add .
       git commit -m "update Android_SDK to ${ANDROIDSDK_V} on LegacyUnity" 
       git pull
       if [[ $? -eq 0 ]] ; then
        git push origin LegacyUnity
       else
echo "failed:在LegacyUnity分支commit后git pull失败，请确认检查"
        exit 7
       fi
       
       if [[ $? -eq 0 ]] ; then
        echo -e "\n已成功将Android_SDK的v${ANDROIDSDK_V}版本更新到PicoSDK_Unity的MainUnity52和LegacyUnity分支中，请继续其他更新SDK的操作，或者开始打包unitypackage..."
       else
        echo "git push到LegacyUnity失败"
       fi
else
       echo "已选择不更新Android_SDK"
fi




echo "开始集成PC_SDK部分..."
if [[ -n ${PCSDK_V} ]] ; then

       
       #拷贝Runtime的PicoPlugin.dll到PicoSDK_Unity的MainUnity52分支之前,更新Unity的代码，
#       cd /data/local/PicoSDK_Unity
        cd D:/scmbuild/PicoSDK_Unity/
       git checkout . && git clean -xdf && git reset --hard HEAD && git checkout MainUnity52 && git pull
       #判断是否更新成功，成功则打印最新5条提交的commit时间、哈希和提交信息;失败则退出脚本并返回2
       if [[ $? -eq 0 ]] ; then
       	echo -e "\n已更新PicoSDK_Unity的MainUnity52分支，最近的5条提交是:
`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
        else
        	echo -e "\nfailed:git pull MainUnity52分支失败！"
               exit 2
       fi
       #开始拷贝
       cp -f ../PicoSDK_PC_Runtime/build/EN/PicoVR/Bin/Win32/PicoPlugin.dll Assets/Plugins/x86/PicoPlugin.dll
       cp -f ../PicoSDK_PC_Runtime/build/EN/PicoVR/Bin/Win64/PicoPlugin.dll Assets/Plugins/x86_64/PicoPlugin.dll
       git add .
       git commit -m "update PC_SDK to ${PCSDK_V} on MainUnity52"
       git pull
       if [[ $? -eq 0 ]] ; then
       	git push origin MainUnity52
       else
       	echo "failed:在MainUnity52分支commit后git pull失败，请确认检查"
       	exit 5
       fi
       git checkout . && git clean -xdf && git checkout LegacyUnity && git pull
       if [[ $? -eq 0 ]] ; then
               echo -e "\n已更新PicoSDK_Unity的LegacyUnity分支，当前所在提交:
`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
        else
                echo -e "\nfailed:git pull LegacyUnity分支失败！"
       	 exit 6
       fi
       cp -f ../PicoSDK_PC_Runtime/build/EN/PicoVR/Bin/Win32/PicoPlugin.dll Assets/Plugins/x86/PicoPlugin.dll
       cp -f ../PicoSDK_PC_Runtime/build/EN/PicoVR/Bin/Win64/PicoPlugin.dll Assets/Plugins/x86_64/PicoPlugin.dll
       git add .
       git commit -m "update PC_SDK to ${PCSDK_V} on LegacyUnity" 
       git pull
       if [[ $? -eq 0 ]] ; then
       	git push origin LegacyUnity
       else
       	echo "failed:在LegacyUnity分支commit后git pull失败，请确认检查"
       	exit 7
       fi
       
       if [[ $? -eq 0 ]] ; then
       	echo -e "\n已成功将PC_SDK的v${PCSDK_V}版本更新到PicoSDK_Unity的MainUnity52和LegacyUnity分支中，请继续其他更新SDK的操作，或者开始打包unitypackage..."
       else
       	echo "git push到LegacyUnity失败"
       fi
else
       echo "已选择不更新PC_SDK"
fi

echo 任意键继续...
char=`get_char`
